package com.lb.jwitter.service;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import javax.jws.WebService;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.lb.jwitter.*;
import com.lb.jwitter.hibernate.*;
import com.lb.jwitter.util.ChumanoLogger;
import com.lb.jwitter.util.ConditionReport;

@Service("tbl_DKMH_DotDK_HocVienService")
@Transactional
@WebService(endpointInterface = "com.lb.jwitter.service.Tbl_DKMH_DotDK_HocVienService")
public class Tbl_DKMH_DotDK_HocVienServiceImpl implements
		Tbl_DKMH_DotDK_HocVienService, Serializable {

	private Log log = LogFactory
			.getLog(Tbl_DKMH_DotDK_HocVienServiceImpl.class);
	private static final long serialVersionUID = -8740095022264495229L;

	@Autowired
	@Qualifier("tbl_DKMH_DotDK_HocVienDAO")
	private Tbl_DKMH_DotDK_HocVienDAO _tbl_DKMH_DotDK_HocVienDao;

	@Autowired
	@Qualifier("tbl_DKMH_DotDKDAO")
	private Tbl_DKMH_DotDKDAO _tbl_DKMH_DotDKDao;

	@Autowired
	@Qualifier("tbl_ThongTin_HocVienDAO")
	private Tbl_ThongTin_HocVienDAO _tbl_ThongTin_HocVienDao;

	@Autowired
	@Qualifier("tbl_CanBoDAO")
	private Tbl_CanBoDAO _tbl_CanBoDao;

	public final List<Tbl_DKMH_DotDK_HocVien> findAlltbl_DKMH_DotDK_HocVien(
			int pageSize, int page) {
		List<Tbl_DKMH_DotDK_HocVien> result = get_tbl_DKMH_DotDK_HocVienDao()
				.findAll(pageSize, page);
		log.info("Number of user in DB:: " + result.size());
		return result;
	}

	@Transactional(noRollbackFor = IllegalArgumentException.class)
	public final Long createtbl_DKMH_DotDK_HocVien(Tbl_DKMH_DotDK_HocVien obj) {
		return _tbl_DKMH_DotDK_HocVienDao.save(obj);
	}

	public final void updatetbl_DKMH_DotDK_HocVien(Tbl_DKMH_DotDK_HocVien obj) {
		_tbl_DKMH_DotDK_HocVienDao.update(obj);
	}// end update methode

	public final Tbl_DKMH_DotDK_HocVien gettbl_DKMH_DotDK_HocVien(Long id) {
		log.info("Try to retrieve the tbl_DKMH_DotDK_HocVien: " + id);
		return get_tbl_DKMH_DotDK_HocVienDao().get(id);
	}

	public void deletetbl_DKMH_DotDK_HocVien(Long id) {
		log.info("Try to delete the tbl_DKMH_DotDK_HocVien by Id : " + id);
		if (_tbl_DKMH_DotDK_HocVienDao.exists(id)) {
			_tbl_DKMH_DotDK_HocVienDao.delete(id);
		} else {
			throw new IllegalArgumentException("EmployeeId '" + id
					+ "' does not exists");
		}
	}

	public void deleteListtbl_DKMH_DotDK_HocVien(String[] listId) {
		for (String id : listId) {
			deletetbl_DKMH_DotDK_HocVien(Long.parseLong(id));
		}
	}

	public String createListtbl_DKMH_DotDK_HocVienByJson(int dotDKId,
			String data, Tbl_TaiKhoan taiKhoan) {
		String reString = "";
		try {
			ChumanoLogger.init(Tbl_DKMH_DotDK_HocVienServiceImpl.class.getName());
			ChumanoLogger.log("0BAT DAU INSERT DU LIEU");
			   
			List<Tbl_DKMH_DotDK_HocVien> tbl_DKMH_DotDK_HocViens = new ArrayList<Tbl_DKMH_DotDK_HocVien>();
			
			JSONArray jsons = (JSONArray) JSONSerializer.toJSON(data);

			for (int i = 0; i < jsons.size(); i++) {
				String logStr ="";
				JSONObject obj = jsons.getJSONObject(i);
				//---------------------------------
				//Lay du lieu
				String mssv = obj.getString("MSSV");
				
				Boolean khongDK = Boolean.parseBoolean(obj.getString("KHONGDANGKY").trim());
				String khongDKLyDo = obj.getString("KHONGDANGKYLYDO");
				
				logStr ="[ " + (i+1)+ " - " 
				+ "MSSV: " + mssv + " - " 
				+ "KHONGDK: " + khongDK + " - " 
				+ "KHONGDKLyDo: " + khongDKLyDo
				
				+"]";
				//----------------------------------
				//==================================
				Tbl_DKMH_DotDK_HocVien tblDKMHDotDKHocVien = new Tbl_DKMH_DotDK_HocVien();

				//tim hoc vien
				List<Long> hocVienId = findHocVienId(mssv);
				
				// List<Tbl_ThongTin_HocVien> hocviens =
				// _tbl_ThongTin_HocVienDao.findByCondition(Tbl_ThongTin_HocVien.class,
				// cons, 0, 0);
				// set hocvien
				if (hocVienId != null && hocVienId.size() > 0) {
					Tbl_ThongTin_HocVien hocvien = new Tbl_ThongTin_HocVien();
					hocvien.setId(hocVienId.get(0));
					tblDKMHDotDKHocVien.setHocVienId(hocvien);

					// set dotDK
					Tbl_DKMH_DotDK tblDKMHDotDK = new Tbl_DKMH_DotDK();
					tblDKMHDotDK.setId(new Long(dotDKId));
					tblDKMHDotDKHocVien.setDotDKId(tblDKMHDotDK);

					// set Can Bo
					tblDKMHDotDKHocVien.setCanBoId(taiKhoan.getCanBoId());

					tblDKMHDotDKHocVien.setKhongDangKyLyDo(khongDKLyDo);
					tblDKMHDotDKHocVien.setKhongDangKyYN(khongDK);
					tblDKMHDotDKHocVien.setSoTCDangKyToiDa(21);
					// set ngay cap nhat
					tblDKMHDotDKHocVien.setNgayCapNhat(new Date());

					tbl_DKMH_DotDK_HocViens.add(tblDKMHDotDKHocVien);
				} else {
					reString += " MSSV kh\u00F4ng t\u00F4\u0300n ta\u0323i: " + obj.getString("MSSV") + "<br/>";
					logStr +=" NOT MSSV";
				}
				
				//LOGGING
		        ChumanoLogger.log(logStr);

			}// end for
			_tbl_DKMH_DotDK_HocVienDao.saveOrUpdateAll(tbl_DKMH_DotDK_HocViens);
		}
		// System.out.println("jsonSize = " + tbl_DKMH_DotDK_HocViens.size());
		catch (Exception e) {
			ChumanoLogger.log("ERROR: " +e.getMessage());
			   System.out.println("------------ERROR CHUMANO-------------");
			   e.printStackTrace();
			   System.out.println("=================================");
		}
		ChumanoLogger.stop();
		
		return reString;
	}

	public void createListtbl_DKMH_DotDK_HocVien(
			List<Tbl_DKMH_DotDK_HocVien> objs) {
		log.info("Try to insert list the tbl_DKMH_DotDK_HocVien");
		if (objs != null) {
			_tbl_DKMH_DotDK_HocVienDao.saveOrUpdateAll(objs);
		} else {
			throw new IllegalArgumentException("Insert List Error");

		}

	}

	public Collection<Tbl_DKMH_DotDK_HocVien> filterListtbl_DKMH_DotDK_HocVienBySoTinChiConNo(
			List<Tbl_DKMH_DotDK_HocVien> objs, int tuSoTinChiNo,
			int denSoTinChiNo) {
		return _tbl_DKMH_DotDK_HocVienDao.filterBySoTinChiConNo(objs,
				tuSoTinChiNo, denSoTinChiNo);
	}

	public List<Tbl_DKMH_DotDK_HocVien> findtbl_DKMH_DotDK_HocVienByCondition(
			List<ConditionReport> cons, int pageSize, int page) {
		return _tbl_DKMH_DotDK_HocVienDao.findByCondition(
				Tbl_DKMH_DotDK_HocVien.class, cons, pageSize, page);
	}

	public int counttbl_DKMH_DotDK_HocVienByCondition(List<ConditionReport> cons) {
		return _tbl_DKMH_DotDK_HocVienDao.countByCondition(
				Tbl_DKMH_DotDK_HocVien.class, cons);
	}

	public final Tbl_DKMH_DotDK_HocVienDAO get_tbl_DKMH_DotDK_HocVienDao() {
		return _tbl_DKMH_DotDK_HocVienDao;
	}

	public final void set_tbl_DKMH_DotDK_HocVienDao(
			Tbl_DKMH_DotDK_HocVienDAO obj) {
		this._tbl_DKMH_DotDK_HocVienDao = obj;
	}

	public Tbl_DKMH_DotDKDAO get_tbl_DKMH_DotDKDao() {
		return _tbl_DKMH_DotDKDao;
	}

	public void set_tbl_DKMH_DotDKDao(Tbl_DKMH_DotDKDAO _tbl_DKMH_DotDKDao) {
		this._tbl_DKMH_DotDKDao = _tbl_DKMH_DotDKDao;
	}

	public Tbl_ThongTin_HocVienDAO get_tbl_ThongTin_HocVienDao() {
		return _tbl_ThongTin_HocVienDao;
	}

	public void set_tbl_ThongTin_HocVienDao(
			Tbl_ThongTin_HocVienDAO _tbl_ThongTin_HocVienDao) {
		this._tbl_ThongTin_HocVienDao = _tbl_ThongTin_HocVienDao;
	}

	public Tbl_CanBoDAO get_tbl_CanBoDao() {
		return _tbl_CanBoDao;
	}

	public void set_tbl_CanBoDao(Tbl_CanBoDAO _tbl_CanBoDao) {
		this._tbl_CanBoDao = _tbl_CanBoDao;
	}
	
	private List<Long> findHocVienId(String ma) {
		List<ConditionReport> cons = new ArrayList<ConditionReport>();
		{
			ConditionReport con = new ConditionReport();
			con.setCol("MSSV");
			con.setValue1(ma);
			con.setOperator("eq");
			cons.add(con);
		}
		List<String> columns = new ArrayList<String>();
		columns.add("id");

		List<Long> ids = _tbl_ThongTin_HocVienDao.findColumnByCondition(Tbl_ThongTin_HocVien.class, cons, columns, 1, 1);
		return ids;
	}
}// endclass

